﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using MineSweeper.EventManagers;
using System.Windows.Input;

namespace MineSweeper.Behaviors
{
    public static class MouseBehavior
    {
        private class WeakEventListener : IWeakEventListener
        {
            bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
            {
                if (managerType == typeof(UIElementPreviewMouseLeftButtonDownEventManager))
                {
                    ((UIElement)sender).SetValue(IsMouseLeftButtonDownProperty, true);
                }
                else if (managerType == typeof(UIElementPreviewMouseLeftButtonUpEventManager))
                {
                    ((UIElement)sender).SetValue(IsMouseLeftButtonDownProperty, false);
                }
                else if (managerType == typeof(UIElementMouseLeaveEventManager))
                {
                    ((UIElement)sender).SetValue(IsMouseLeftButtonDownProperty, false);
                }
                else if (managerType == typeof(UIElementMouseEnterEventManager))
                {
                    ((UIElement)sender).SetValue(IsMouseLeftButtonDownProperty, Mouse.LeftButton == MouseButtonState.Pressed);
                }
                else
                {
                    return false;
                }
                return true;
            }
        }

        private static readonly WeakEventListener weakEventListener = new WeakEventListener();



        public static readonly DependencyProperty IsMouseLeftButtonDownProperty =
        DependencyProperty.RegisterAttached(
        "IsMouseLeftButtonDown",
        typeof(bool),
        typeof(MouseBehavior),
        new FrameworkPropertyMetadata(false, null));

        public static readonly DependencyProperty MonitorMouseProperty =
        DependencyProperty.RegisterAttached(
        "MonitorMouse",
        typeof(bool),
        typeof(MouseBehavior),
        new FrameworkPropertyMetadata(false,
            FrameworkPropertyMetadataOptions.NotDataBindable,
            (d, e) =>
            {
                var element = (UIElement)d;
                if ((bool?)e.NewValue ?? false == true)
                {
                    UIElementPreviewMouseLeftButtonDownEventManager.AddListener(
                        element, weakEventListener);

                    UIElementPreviewMouseLeftButtonUpEventManager.AddListener(
                        element, weakEventListener);

                    UIElementMouseLeaveEventManager.AddListener(
                        element, weakEventListener);

                    UIElementMouseEnterEventManager.AddListener(
                        element, weakEventListener);
                }
                else
                {
                    UIElementPreviewMouseLeftButtonDownEventManager.RemoveListener(
                        element, weakEventListener);

                    UIElementPreviewMouseLeftButtonUpEventManager.RemoveListener(
                        element, weakEventListener);

                    UIElementMouseLeaveEventManager.RemoveListener(
                        element, weakEventListener);

                    UIElementMouseEnterEventManager.RemoveListener(
                       element, weakEventListener);
                }
            })
        );

        public static bool GetIsMouseLeftButtonDown(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsMouseLeftButtonDownProperty);
        }

        public static void SetMonitorMouse(DependencyObject obj, bool value)
        {
            obj.SetValue(MonitorMouseProperty, value);
        }

    }
}
